package com.jane.gzzb;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jane.bean.ChuShouBean;
import com.jane.bean.DouYuBean;
import com.jane.bean.QuanMinBean;
import com.jane.bean.XiongMaoBean;
import com.jane.config.NetConfig;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import tv.danmaku.ijk.media.widget.media.AndroidMediaController;
import tv.danmaku.ijk.media.widget.media.IjkVideoView;

public class PlayActivity extends AppCompatActivity {
    private IjkVideoView ijk;
    private String sourcename;
    private String roomid;
    private String urlPath;
    private String playPath;

    private QuanMinBean quanMinBean;
    private XiongMaoBean xiongMaoBean;
    private DouYuBean douYuBean;
    private ChuShouBean chuShouBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);
        //接收qitaActivity传递过来的值
        Intent intent = getIntent();
        if(intent!=null){
            roomid=intent.getStringExtra("roomid");
            sourcename=intent.getStringExtra("sourcename");
        }

        innitView();
        initData();
    }

    private void innitView() {
        //初始化
        IjkMediaPlayer.loadLibrariesOnce(null);
        IjkMediaPlayer.native_profileBegin("libijkplayer.so");

        ijk= (IjkVideoView) findViewById(R.id.ijk_play);
    }

    private void initData() {
        if(sourcename!=null&&roomid!=null) {
            switch (sourcename) {
                case "全民":
                    urlPath = String.format(NetConfig.QUANMIN, roomid);
                    break;
                case "熊猫":
                    urlPath=String.format(NetConfig.XIONGMAO,roomid);
                    break;
                case "斗鱼":
                    urlPath=String.format(NetConfig.DOUYU,roomid);
                    break;
                case "触手":
                    urlPath=String.format(NetConfig.CHUSHOU,roomid);
                    break;
                default:
                    break;
            }
//            Log.e("urlPath",urlPath);
            loadData();
        }
    }

    private void loadData() {
        RequestParams params=new RequestParams(urlPath);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                switch (sourcename){
                    case "全民":
                        parseQuanMin(result);
                        break;
                    case "熊猫":
                        parseXiongMao(result);
                        break;
                    case "斗鱼":
                        parseDouYu(result);
                        break;
                    case "触手"://因为时间戳无法获取数据
                        parseChuShou(result);
                        break;
                    default:
                        break;
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {}
            @Override
            public void onFinished() {}
        });
    }

    //解析全民数据
    private void parseQuanMin(String result) {
        try {
            JSONObject jsonObject=new JSONObject(result);


            String uid = jsonObject.optString("uid");
            String title = jsonObject.optString("title");
            String nick = jsonObject.optString("nick");
            String category_name = jsonObject.optString("category_name");

            JSONObject live = jsonObject.getJSONObject("live");
            JSONObject ws = live.getJSONObject("ws");
            JSONObject hls = ws.getJSONObject("hls");

            quanMinBean=new QuanMinBean();

            quanMinBean.setUid(uid);
            quanMinBean.setTitle(title);
            quanMinBean.setCategory_name(category_name);
            quanMinBean.setNick(nick);
            try{
                //获取标清的地址
                JSONObject jsonObject3 = hls.getJSONObject("3");
                String biaoQingPath=jsonObject3.optString("src");
//                Log.e("标清",biaoQingPath);

                quanMinBean.setBiaoQingPath(biaoQingPath);
            }catch (JSONException e){
                e.printStackTrace();
            }

            try{
                //获取超清的地址
                JSONObject jsonObject0 = hls.getJSONObject("0");
                String chaoQingPath=jsonObject0.optString("src");
//                Log.e("超清",chaoQingPath);

                quanMinBean.setChaoQingPath(chaoQingPath);
            }catch (JSONException e){
                e.printStackTrace();
            }

            try{
                //获取高清的地址
                JSONObject jsonObject4 = hls.getJSONObject("4");
                String gaoQingPath=jsonObject4.optString("src");
//                Log.e("高清'",gaoQingPath);
                quanMinBean.setGaoQingPath(gaoQingPath);


                //设置播放的标清/高清/超清的播放路径
//                Log.e("quanMinBean",quanMinBean.toString());
            }catch (JSONException e){
                e.printStackTrace();
            }

        } catch (Exception e) {
            Log.e("解析", "解析错误----------");
            e.printStackTrace();
        }
        setPlay();
    }

    //解析熊猫数据
    private void parseXiongMao(String result) {
        String jsonStr=result.substring(result.indexOf("(")+1,result.lastIndexOf(")"));
        xiongMaoBean = new Gson().fromJson(jsonStr,XiongMaoBean.class);
        setPlay();
    }

    //解析斗鱼数据
    private void parseDouYu(String result) {
        douYuBean = new Gson().fromJson(result,DouYuBean.class);
        setPlay();
    }
    //解析触手数据
    private void parseChuShou(String result) {
        chuShouBean=new Gson().fromJson(result,ChuShouBean.class);
        setPlay();
    }
    //设置开始播放
    private void setPlay() {
        switch (sourcename){
            case "全民":
                if(quanMinBean.getBiaoQingPath()!=null){
                    playPath=quanMinBean.getBiaoQingPath();
                }else if(quanMinBean.getGaoQingPath()!=null){
                    playPath=quanMinBean.getGaoQingPath();
                }else if(quanMinBean.getChaoQingPath()!=null){
                    playPath=quanMinBean.getChaoQingPath();
                }
                break;
            case "熊猫":
                if(xiongMaoBean.getData().getVideoinfo().getAddress()!=null){
                    playPath=xiongMaoBean.getData().getVideoinfo().getAddress();
                }
                break;
            case "斗鱼":
                if(douYuBean.getData().getHls_url()!=null){
                    playPath=douYuBean.getData().getHls_url();
                }
                break;
            case "触手":
                if(chuShouBean.getData().get(0).getShdPlayUrl()!=null){
                    playPath=chuShouBean.getData().get(1).getShdPlayUrl();
                }
                break;
            default:
                break;
        }
        // 设置控制栏
        AndroidMediaController controller = new AndroidMediaController(this);
        ijk.setMediaController(controller);

        //设置路径并开始播放
        //TODO 设置播放路径

        Log.e("playpath",playPath);
        if(playPath!=null){
            ijk.setVideoPath(playPath);
            ijk.start();
        }else{
            Toast.makeText(this, "播放出错,请重试...", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁
//        if (!ijk.isBackgroundPlayEnabled()) {
            ijk.stopPlayback();
            ijk.release(true);
            ijk.stopBackgroundPlay();
//        } else {
//            ijk.enterBackground();
//        }
        IjkMediaPlayer.native_profileEnd();
    }
}
